<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body {margin: 0;overflow: hidden;}
    #canvas {background-color: black;}
  </style>
</head>
<body>
  <canvas id="canvas"></canvas>
  <script>
    /** @type {HTMLCanvasElement} */

    const colors = ['red', 'yellow'];

    function draw () {
      const canvas = document.querySelector('#canvas');
      canvas.width = window.innerWidth;
      canvas.height = window.innerHeight;
      const ctx = canvas.getContext('2d');
      // 爱心
      ctx.save();
      ctx.translate(300,400);
      ctx.beginPath();
      ctx.moveTo(0,0);
      ctx.bezierCurveTo(-200,-50,-180,-300,0,-200);
      ctx.bezierCurveTo(180,-300,200,-50,0,0);
      ctx.closePath();
      ctx.strokeStyle = colors[0];
      ctx.lineWidth = 10;
      // 虚线
      ctx.setLineDash([30]);
      ctx.stroke();
      // 第二部分虚线
      ctx.lineDashOffset = -30;
      ctx.strokeStyle = colors[1];
      ctx.stroke();
      ctx.restore();
    }
    
    draw();

    setInterval(() => {
      draw();
      colors.reverse();
    }, 500)


  </script>
</body>
</html>